package com.bfly.mall.product.service.impl;

import com.bfly.common.util.StringUtil;
import com.bfly.core.base.service.BaseServiceImpl;
import com.bfly.mall.product.dao.ProductSkuMapper;
import com.bfly.mall.product.entity.ProductSku;
import com.bfly.mall.product.dto.ProductSkuFormDTO;
import com.bfly.mall.product.service.IProductSkuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 长沙商脉科技有限公司
 * @date 2020/9/21 12:32
 */
@Service
public class ProductSkuServiceImpl extends BaseServiceImpl<ProductSku, Integer> implements IProductSkuService {

    @Autowired
    private ProductSkuMapper skuMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void merge(int productId, List<ProductSkuFormDTO> formDTO) {
        // 保存或编辑
        formDTO.forEach(dto -> {
            ProductSku sku;
            if (StringUtils.isEmpty(dto.getSn())) {
                sku = new ProductSku();
                BeanUtils.copyProperties(dto, sku);

                sku.setProductId(productId);
                sku.setCreateDate(new Date());
                sku.setSn(generateSn());
                sku.setUpdateDate(sku.getCreateDate());
                skuMapper.insert(sku);
            } else {
                sku = getSku(productId, dto.getSn());
                if (sku != null) {
                    sku.setPrice(dto.getPrice());
                    sku.setVipPrice(dto.getPrice());
                    sku.setOriginalPrice(dto.getOriginalPrice());
                    sku.setStock(dto.getStock());
                    sku.setUpdateDate(new Date());
                    skuMapper.updateById(sku);
                }
            }
        });
        super.getCache().clear();
    }

    /**
     * 生成产品SKU编码
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/10/4 21:27
     */
    private String generateSn() {
        String sn;
        Map<String, Object> param = new HashMap<>(1);
        while (true) {
            sn = System.currentTimeMillis() + StringUtil.getRandomNumber(8);
            param.put("sn", sn);
            long count = getCount(param);
            if (count == 0) {
                break;
            }
        }
        return sn;
    }

    @Override
    public ProductSku getSku(int productId, String sn) {
        return skuMapper.getSku(productId, sn);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementProductStock(int productId, String sn, int setup) {
        skuMapper.incrementProductStock(productId, sn, setup);
    }

    @Override
    public List<ProductSku> getProductSku(int productId) {
        Map<String,Object> param=new HashMap<>();
        param.put("product_id",productId);
        return super.getList(param);
    }
}